# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
### Reducing Prejudice and Support for Religious 
### Nationalism Through Conversations on WhatsApp 

### Author: Rajeshwari Majumdar
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# File:     results_mechanisms.R
# Purpose:  analyze mechanisms; produce Figure 1
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# Load data and custom functions 
rm(list=ls())
load("data/Completes_Merged_Anonymized.RData")
source("code/02_setup_functions.R")


# Reverse code attributes variables for easier plotting
recode_atts = function(var){
  recode(d[,var], 
         "1" = 5,
         "2" = 4,
         "4" = 2,
         "5" = 1)
}
attsvars = c("post.gr_patriot_muslim", "post.gr_peace_muslim", 
             "post.gr_hardwork_muslim", "post.gr_honest_muslim", 
             "follow.gr_patriot_muslim", "follow.gr_peace_muslim", 
             "follow.gr_hardwork_muslim", "follow.gr_honest_muslim")
for (i in 1:length(attsvars)){
  d[,attsvars[i]] = recode_atts(attsvars[i])
}

d$post.total.outgr = (d$post.gr_patriot_muslim + d$post.gr_peace_muslim + 
                        d$post.gr_hardwork_muslim + d$post.gr_honest_muslim)
d$post.avg.outgr = d$post.total.outgr / 4

d$follow.total.outgr = (d$follow.gr_patriot_muslim + d$follow.gr_peace_muslim + 
                          d$follow.gr_hardwork_muslim + d$follow.gr_honest_muslim)
d$follow.avg.outgr = d$follow.total.outgr / 4

rm(i, attsvars, recode_atts)


# Create list of mechanism variables and their labels 
mechanism_vars = c("know_muslimvictims","discrim_muslim",
                   "happy_muslimbooked","symp_muslim",
                   "post.avg.outgr","follow.avg.outgr",
                   "comf_work_muslim","twt_Housing_q1")
mechanism_vars_labels = c("[A] Knowledge of hate crimes (immediate)",
                          "[B] Knowledge of discrimination (2 weeks later)",
                          "[C] Happiness about Love Jihad arrest (immediate)",
                          "[D] Sympathy about discrimination (2 weeks later)",
                          "[E] Negative attributes index (immediate)",
                          "[F] Negative attributes index (2 weeks later)",
                          "[G] Comfort working together (immediate)",
                          "[H] Approval for segregation (3 weeks later)"
                          ) 


# Estimate models for each topic
results = rbind(
  fn_extract_coefs(depvars = mechanism_vars, depvar_labels = mechanism_vars_labels, topic = "non", scalevars = TRUE, data = d),
  fn_extract_coefs(depvars = mechanism_vars, depvar_labels = mechanism_vars_labels, topic = "gen", scalevars = TRUE, data = d),
  fn_extract_coefs(depvars = mechanism_vars, depvar_labels = mechanism_vars_labels, topic = "igr", scalevars = TRUE, data = d)
)


# Prepare the results dataframe for plotting
results$ymin = results$estimate - qnorm(.975)*results$std.error
results$ymax = results$estimate + qnorm(.975)*results$std.error

results$topic = factor(results$topic, levels = c("non","gen","igr"))

results = results %>% 
  mutate(
    category = case_when(
      depvar %in% c("know_muslimvictims","discrim_muslim") ~ "Knowledge of discrimination",
      depvar %in% c("happy_muslimbooked","symp_muslim") ~ "Sympathy about discrimination",
      depvar %in% c("post.avg.outgr","follow.avg.outgr") ~ "Belief in common negative stereotypes",
      depvar %in% c("comf_work_muslim","twt_Housing_q1") ~ "Anxiety about potential future interactions"
    ),
    category = factor(
      category,
      levels = c(
        "Knowledge of discrimination",
        "Sympathy about discrimination",
        "Belief in common negative stereotypes",
        "Anxiety about potential future interactions"
      )
    )
  )

results$depvar = fct_rev(factor(results$depvar, 
                                levels = mechanism_vars,
                                labels = mechanism_vars_labels
))

results$signif05 = factor(ifelse(results$p.value < 0.05, 1, 0))


# Plot
ggplot(results, aes(x = estimate, y = depvar, shape = topic, group = topic, color = signif05)) +
  geom_point(size = 3, position = position_dodge(.6)) +
  geom_errorbar(aes(xmin = ymin, xmax  = ymax), position = position_dodge(.6)) +
  facet_col( .~category, scales = "free_y", space = "free") +
  geom_vline(xintercept = 0, lty = 2, col = "red") +
  scale_x_continuous(limits = c(-1,1), breaks = seq(-1,1,0.2)) +
  scale_shape_manual("Conversation topic", values = c(16,0,2), labels = c("Non-pol.","Gen. pol.","IGR")) +
  scale_color_manual("Statistical significance",values = c("gray", "blue"), labels = c("p-value > 0.05","p-value < 0.05")) +
  labs(x = "Standardized treatment effect of Hindu-Muslim pair", y = "Mechanism") + 
  theme_bw() +
  theme(legend.position = "bottom", 
        legend.box = "vertical", legend.margin = margin(),
        legend.spacing.y = unit(0, "mm"), 
        legend.box.background = element_rect(colour = "black"),
        legend.box.margin = margin(t = 2, l = 4, r = 4, b = 2),
        axis.text = element_text(face="bold"),
        strip.text = element_text(face = "bold")
  )
